
/*
The following code replicate Table J1 in the Appendix

*/


*Part IV Heterogeneous changes --(multi-tech)
use "D:\Nanjing\2019\decomposition_final\data\Multiple_technology",clear


bysort frdm year : egen ann_so2=sum(so2_emission)
bysort frdm year : egen ann_rev=sum(totaloutput)
gen so2_int=ann_so2/ann_rev


egen ub1=pctile(so2_int), p(90)
egen ub2=pctile(so2_int), p(75)
egen ub3=pctile(so2_int), p(70)
egen ub4=pctile(so2_int), p(60)
egen ub5=pctile(so2_int), p(50)
egen ub6=pctile(so2_int), p(40)
egen ub7=pctile(so2_int), p(30)
egen ub8=pctile(so2_int), p(20)
egen ub9=pctile(so2_int), p(5)

gen rank=.
replace rank=1 if so2_int<ub9

replace rank=2 if so2_int>=ub9&so2_int<ub8
replace rank=3 if so2_int>=ub8&so2_int<ub7
replace rank=4 if so2_int>=ub7&so2_int<ub6
replace rank=5 if so2_int>=ub6&so2_int<ub5
replace rank=6 if so2_int>=ub5&so2_int<ub4
replace rank=7 if so2_int>=ub4&so2_int<ub3
replace rank=8 if so2_int>=ub3&so2_int<ub2
replace rank=9 if so2_int>=ub2

replace rank=10 if so2_int>ub1


*replace dirty=1 if prod_2==1|prod_2==2|prod_2==3|prod_2==7|prod_2==8|prod_2==11|prod_2==19|prod_2==20|prod_2==21|prod_2==22|prod_2==25|prod_2==26
gen cost=exp(m)
bysort rank year: egen tot_rev=sum(totaloutput) /*annual level total output*/
bysort rank year: egen tot_cost=sum(cost)       /*annual level total cost*/

gen rev_share=totaloutput/tot_rev
gen cost_share=cost/tot_cost
*drop if Markup_firm<0

capture drop ub
capture drop lb
bysort rank year: egen ub=pctile(Markup_firm),p(99)
bysort rank year: egen lb=pctile(Markup_firm),p(1)
drop if Markup_firm>ub|Markup_firm<lb


*------------------------------------------
*4. cost weighted average of markup in each year
*------------------------------------------
bysort rank year: egen cost_Markup=sum(cost_share*Markup_firm)

*------------------------------------------
*5. variance of markup
*------------------------------------------
bysort rank year: egen sd=sd(Markup_firm)



keep if year==2000|year==2004|year==2005
gen indc=.
replace indc=1 if year==2000
replace indc=2 if year==2005|year==2004


duplicates drop rank indc, force

list rank indc cost_Markup



tsset rank indc

bysort rank: gen cost_Markup_l=l.cost_Markup
bysort rank: gen sd_l=l.sd

gen diff_markup=(cost_Markup-cost_Markup_l)/cost_Markup
gen diff_sd=(sd-sd_l)/sd

bysort rank: sum diff_markup diff_sd cost_Markup_l








*****************************************************************************
*sector_level results
*



**********************
*Definition 2:


*Part IV Heterogeneous changes --(multi-tech)
use "D:\Nanjing\2019\decomposition_data_file\code for submission\Multiple_technology",clear


bysort prod_2code year : egen ann_so2=sum(so2_emission*c_x) if year==2000
bysort prod_2code year : egen ann_rev=sum(totaloutput*c_x) if year==2000
gen so2_int=ann_so2/ann_rev

bysort prod_2code: egen so2_new=max(so2_int)
drop so2_int
rename so2_new so2_int

gen rank=.
replace rank=1 if prod_2code==43|prod_2code==9
replace rank=2 if prod_2code==18
replace rank=3 if prod_2code==30|prod_2code==26|prod_2code==25
replace rank=4 if prod_2code==39|prod_2code==36|prod_2code==29
replace rank=5 if prod_2code==6|prod_2code==5|prod_2code==3|prod_2code==14
replace rank=6 if prod_2code==21|prod_2code==22|prod_2code==16
replace rank=7 if prod_2code==7|prod_2code==8
replace rank=8 if prod_2code==11|prod_2code==1|prod_2code==20|prod_2code==19

replace rank=9 if prod_2code==2|prod_2code==.

drop if rank==.
/*
egen ub1=pctile(so2_int), p(90)
egen ub2=pctile(so2_int), p(75)
egen ub3=pctile(so2_int), p(70)
egen ub4=pctile(so2_int), p(60)
egen ub5=pctile(so2_int), p(50)
egen ub6=pctile(so2_int), p(40)
egen ub7=pctile(so2_int), p(30)
egen ub8=pctile(so2_int), p(20)
egen ub9=pctile(so2_int), p(5)

gen rank=.
replace rank=1 if so2_int<ub9

replace rank=2 if so2_int>=ub9&so2_int<ub8
replace rank=3 if so2_int>=ub8&so2_int<ub7
replace rank=4 if so2_int>=ub7&so2_int<ub6
replace rank=5 if so2_int>=ub6&so2_int<ub5
replace rank=6 if so2_int>=ub5&so2_int<ub4
replace rank=7 if so2_int>=ub4&so2_int<ub3
replace rank=8 if so2_int>=ub3&so2_int<ub2
replace rank=9 if so2_int>=ub2

replace rank=10 if so2_int>ub1
*/

*replace dirty=1 if prod_2==1|prod_2==2|prod_2==3|prod_2==7|prod_2==8|prod_2==11|prod_2==19|prod_2==20|prod_2==21|prod_2==22|prod_2==25|prod_2==26
gen cost=exp(m)

bysort rank year: egen tot_cost=sum(cost*c_x)       /*annual level total cost*/



gen cost_share=cost*c_x/tot_cost
*drop if Markup_firm<0



*------------------------------------------
*4. cost weighted average of markup in each year
*------------------------------------------

bysort  rank year: egen cost_Markup=sum(cost_share*Markup_firm)

*------------------------------------------
*5. variance of markup
*------------------------------------------
bysort rank year: egen sd=sd(Markup_firm)



keep if year==2000|year==2005
gen indc=.
replace indc=1 if year==2000
replace indc=2 if year==2005

sort prod_2code indc
duplicates drop rank indc, force

list rank indc cost_Markup



tsset rank indc

bysort rank: gen cost_Markup_l=l.cost_Markup
bysort rank: gen sd_l=l.sd

gen diff_markup=(cost_Markup-cost_Markup_l)
gen diff_sd=(sd-sd_l)


drop if diff_markup==.
sort rank
list diff_markup diff_sd so2_int rank
correlate diff_markup diff_sd so2_int






*********************************************************

*---------------------------------------------------------
* 3. updated : exclude outliers
*----------------------------------------------------------
************************************************************



*Part IV Heterogeneous changes --(multi-tech)
use "D:\Nanjing\2019\decomposition_data_file\code for submission\Multiple_technology",clear

drop if so2_emission==3651584&prod_2code==9&year==2000

bysort prod_2code year : egen ann_so2=sum(so2_emission*c_x) 
bysort prod_2code year : egen ann_rev=sum(totaloutput*c_x) 
gen so2_int=ann_so2/ann_rev

bysort prod_2code: egen so2_new=max(so2_int)
drop so2_int
rename so2_new so2_int

gen rank=.
replace rank=1 if prod_2code==43|prod_2code==9
replace rank=2 if prod_2code==18
replace rank=3 if prod_2code==30|prod_2code==26|prod_2code==25
replace rank=4 if prod_2code==39|prod_2code==36|prod_2code==29
replace rank=5 if prod_2code==6|prod_2code==5|prod_2code==3|prod_2code==14
replace rank=6 if prod_2code==21|prod_2code==22|prod_2code==16
replace rank=7 if prod_2code==7|prod_2code==8
replace rank=8 if prod_2code==11|prod_2code==20|prod_2code==19

replace rank=9 if prod_2code==2|prod_2code==1|rank==.

drop if rank==.





*keep if rank==2|rank==8



gen cost=exp(m)

bysort rank year: egen tot_cost=sum(cost*c_x)       /*annual level total cost*/
bysort year: egen ann_cost=sum(tot_cost)
bysort year: egen Exp=sum(export)

capture drop ub
*bysort rank : egen ub=pctile(export), p(99.9)
bysort rank year: egen ann_exp=sum(export) 

gen cost_share=cost*c_x/tot_cost
gen cost_share2=tot_cost/ann_cost



bysort rank: egen m_so2_int=mean(so2_int) 
drop so2_int
rename m_so2_int so2_int

*------------------------------------------
*4. cost weighted average of markup in each year
*------------------------------------------

drop if rank==2&year==2005&Markup_firm>4.7
*drop if rank==8&year==2005&Markup_firm<0.1
bysort rank year: egen cost_Markup=sum(cost_share*Markup_firm)

*------------------------------------------
*5. variance of markup
*------------------------------------------
bysort rank year: egen sd=sd(Markup_firm)
bysort year: egen cost_sd=sum(cost_share2*sd)


keep if year==2000|year==2005
gen indc=.
replace indc=1 if year==2000
replace indc=2 if year==2005


bysort year: sum Exp
bysort year: sum cost_sd




sort prod_2code indc
duplicates drop rank indc, force




tsset rank indc

bysort rank: gen cost_Markup_l=l.cost_Markup
bysort rank: gen sd_l=l.sd
bysort rank: gen ann_exp_l=l.ann_exp

gen diff_markup=(cost_Markup-cost_Markup_l)
gen diff_sd=(sd-sd_l)
gen diff_exp=(ann_exp-ann_exp_l)/ann_exp_l

drop if diff_markup==.
sort rank
list rank so2_int diff_markup diff_sd diff_exp   



